package com.bnz.service.Impl;


import com.bnz.dao.ClassesMapper;
import com.bnz.dao.StudentMapper;
import com.bnz.pojo.Student;
import com.bnz.pojo.StudentExample;
import com.bnz.service.StudentService;
import com.bnz.util.PageResult;
import com.bnz.vo.StudentVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private ClassesMapper classesMapper;

    //查询所有学生
    @Override
    public List<Student> findAll() {
        //查询所有学生列表
        List<Student> students = studentMapper.selectByExample(null);
        //设置学生的班级名称
        for (Student student : students) {
            student.setCname(classesMapper.selectByPrimaryKey(student.getCid()).getCname());
        }
        //返回学生列表
        return students;
    }

    //分页查询
    @Override
    public PageResult<Student> findByPage(int page, int pagesize) {
        //开始分页
        PageHelper.startPage(page,pagesize);
        //开始查询
        StudentExample example = new StudentExample();
        List<Student> students = studentMapper.selectByExample(example);
        //设置学生的班级名称
        for (Student student : students) {
            student.setCname(classesMapper.selectByPrimaryKey(student.getCid()).getCname());
        }
        //包装为PageInfo
        PageInfo<Student> pageInfo = new PageInfo<>(students);
        //返回
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
    }

    //分页带条件查询
    @Override
    public PageResult<Student> search(int page, int pagesize, StudentVo vo) {
        //开始分页
        PageHelper.startPage(page,pagesize);
        //开始查询
        StudentExample example = new StudentExample();
        StudentExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(vo.getSname())){
            criteria.andSnameLike("%" + vo.getSname() + "%");
        }
        if(!StringUtils.isEmpty(vo.getAddr())){
            criteria.andAddrLike("%" + vo.getAddr() + "%");
        }
        if(vo.getCid() != 0){
            criteria.andCidEqualTo(vo.getCid());
        }
        List<Student> students = studentMapper.selectByExample(example);
        //设置学生的班级名称
        for (Student student : students) {
            student.setCname(classesMapper.selectByPrimaryKey(student.getCid()).getCname());
        }
        //包装为PageInfo
        PageInfo<Student> pageInfo = new PageInfo<>(students);
        //返回
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
    }

    //添加学生
    @Override
    public void add(Student student) {
        studentMapper.insert(student);
    }

    //修改学生
    @Override
    public void update(Student student) {
        studentMapper.updateByPrimaryKey(student);
    }

    //删除学生
    @Override
    public void delete(List<Integer> sids) {
        //定义查询实例
        StudentExample example = new StudentExample();
        StudentExample.Criteria criteria = example.createCriteria();
        //添加查询条件
        criteria.andSidIn(sids);
        //根据条件删除
        studentMapper.deleteByExample(example);
    }
}
